package com.app;

import com.app.entity.ExcelRowData;
import com.app.entity.TemplateEntity;
import com.app.util.DatabaseUtil;
import com.app.util.ExcelUtils;
import com.app.util.FreemarkerUtil;
import com.app.util.MyStringUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class HtmlGenerator {
	
	//测试运行

    public static void main(String[] args) throws IOException, InvalidFormatException {
        TemplateEntity templateEntity = new TemplateEntity();
        List<ExcelRowData> excelRowDataList = convertExcelToExcelRowData("src/main/resources/模板.xlsx", templateEntity);
        DatabaseUtil.setColumnEntityList(excelRowDataList, templateEntity);
        generatehtml(templateEntity);
        generateTs(templateEntity);
    }



    public static List<ExcelRowData> convertExcelToExcelRowData(String filepath, TemplateEntity templateEntity)
            throws EncryptedDocumentException, InvalidFormatException, IOException {
        Workbook workbook = ExcelUtils.getWorkbook(filepath);
        List<ExcelRowData> excelRowDataList = new ArrayList<>();
        if (workbook != null) {
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet != null) {
                int rowNos = sheet.getLastRowNum();// 得到excel的总记录条数
                for (int i = 0; i <= rowNos; i++) {// 遍历行
                    Row row = sheet.getRow(i);
                    if (i == 0) {
                        continue;
                    }
                    if (row != null) {
                        ExcelRowData excelRowData = new ExcelRowData();
                        int columNos = row.getLastCellNum();// 表头总共的列数
                        for (int j = 0; j < columNos; j++) {
                            Cell cell = row.getCell(j);
                            if (cell != null) {
                                cell.setCellType(CellType.STRING);
                                String stringCellValue = cell.getStringCellValue();
                                switch (j) {
                                    case 0:
                                        break;
                                    case 1:
                                        excelRowData.setFieldChineseName(stringCellValue);
                                        break;
                                    case 2:
                                        if (StringUtils.isNotBlank(stringCellValue)) {
                                            excelRowData.setTableName(stringCellValue.toLowerCase());
                                        }
                                        break;
                                    case 3:
                                        excelRowData.setFieldName(stringCellValue);
                                        break;
                                    case 4:
                                        excelRowData.setCondition(stringCellValue.equalsIgnoreCase("y") ? true : false);
                                        break;
                                    case 5:
                                        if (stringCellValue.contains("文本框")) {
                                            excelRowData.setType(ExcelRowData.TYPE_TEXT);
                                        } else if (stringCellValue.contains("公司代码") && stringCellValue.contains("联想控件")) {
                                            excelRowData.setType(ExcelRowData.TYPE_DROP_BUKRS);
                                        } else if (stringCellValue.contains("工厂代码") && stringCellValue.contains("联想控件")) {
                                            excelRowData.setType(ExcelRowData.TYPE_DROP_WERKS);
                                        } else if (stringCellValue.contains("日期")) {
                                            excelRowData.setType(ExcelRowData.TYPE_DATE);
                                        } else if (stringCellValue.contains("下拉")) {
                                            excelRowData.setType(ExcelRowData.TYPE_SELECT);
                                        } else {
                                            excelRowData.setType(ExcelRowData.TYPE_TEXT);
                                        }
                                        break;
                                    case 6:
                                        templateEntity.setComponentName(stringCellValue);
                                        templateEntity.setCamelComponentName1(MyStringUtils.getCamelCaseString(stringCellValue, true));
                                        templateEntity.setCamelComponentName2(MyStringUtils.getCamelCaseString(stringCellValue, false));
                                        break;
                                    case 7: // 表格是否有联想控件
                                        if (stringCellValue.equals("是")) {
                                            templateEntity.setHasAssociateWidget(true);
                                        } else {
                                            templateEntity.setHasAssociateWidget(false);
                                        }
                                        break;
                                    case 8: // 联想控件名称
                                        templateEntity.setAssociateWidgetName(stringCellValue);
                                        break;
                                    case 9: // 字段是否需要数组模板转换
                                        if (stringCellValue.equals("是")) {
                                            excelRowData.setHasArrayTemplate(true);
                                        } else {
                                            excelRowData.setHasArrayTemplate(false);
                                        }
                                        break;
                                    case 10: // 数组模板转换的数据（JSON数组形式）
                                        excelRowData.setArrayTemplateJSON(stringCellValue);
                                        break;
                                    case 11: // 查询长度
                                        if (StringUtils.isNotBlank(stringCellValue)) {
                                            excelRowData.setMaxLength(Integer.valueOf(stringCellValue));
                                        }
                                        break;
                                }
                            } else {
                                switch (j) {
                                    case 5:
                                        excelRowData.setType(ExcelRowData.TYPE_TEXT);
                                        break;
                                }
                            }
                        }
                        excelRowDataList.add(excelRowData);
                    }

                }

            }
        }
        return excelRowDataList;
    }

    public static void generatehtml(TemplateEntity templateEntity) {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
        try {
            //  指定模板所在的classpath目录
            cfg.setClassForTemplateLoading(HtmlGenerator.class, "/template");
            //  指定模板
            Template t = cfg.getTemplate("angularhtmlTemplate.ftl");
            String projectDirectory = "mybatis-generator-core/generatorCode/resources/";
            File f = new File(System.getProperty("user.dir") + "/" + projectDirectory);
            if (!f.exists()) {
                f.mkdirs();
            }
            String filePath = System.getProperty("user.dir") + "/" + projectDirectory + "app.html";
            File file = new File(filePath);
            FreemarkerUtil.generateFreemarkerFile(file, t, templateEntity);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void generateTs(TemplateEntity templateEntity) {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
        try {
            //  指定模板所在的classpath目录
            cfg.setClassForTemplateLoading(HtmlGenerator.class, "/template");
            //  指定模板
            Template t = cfg.getTemplate("tsTemplate.ftl");
            String projectDirectory = "mybatis-generator-core/generatorCode/resources/";
            File f = new File(System.getProperty("user.dir") + "/" + projectDirectory);
            if (!f.exists()) {
                f.mkdirs();
            }
            String filePath = System.getProperty("user.dir") + "/" + projectDirectory + "ts.ts";
            File file = new File(filePath);
            FreemarkerUtil.generateFreemarkerFile(file, t, templateEntity);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
